package com.nlx.notes.module.service.impl.login;

import cn.hutool.core.util.StrUtil;
import com.nlx.notes.module.bean.dto.login.GuestLoginDTO;
import com.nlx.notes.module.bean.dto.login.UserLoginDTO;
import com.nlx.notes.module.bean.dto.querydb.UserQueryDbDTO;
import com.nlx.notes.module.bean.vo.login.UserLoginVo;
import com.nlx.notes.module.bean.vo.login.UserTokenVO;
import com.nlx.notes.module.core.consts.RedisConst;
import com.nlx.notes.module.core.enums.error.LoginCodeEnum;
import com.nlx.notes.module.entity.Users;
import com.nlx.notes.module.service.IUserLoginRecordService;
import com.nlx.notes.module.service.user.IUsersService;
import com.nlx.notes.module.service.login.LoginService;
import com.nlx.notes.module.service.login.RegisterService;
import com.nlx.notes.support.handler.exception.BusinessException;
import com.nlx.notes.support.helper.login.TokenStoreHelper;
import com.nlx.notes.support.helper.redis.RedisHelper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

@Slf4j
@Service
@RequiredArgsConstructor
public class UserLoginServiceImpl  implements LoginService {


    final RedisHelper redisHelper;

    final IUsersService usersService;

    final TokenStoreHelper tokenStoreHelper;

    final RegisterService registerUser;

    final IUserLoginRecordService userLoginRecordService;



    @Override
    public UserLoginVo login(UserLoginDTO userLoginDTO) {
        //判断验证码是否正确
        String codeKey = RedisConst.Token.USER_LOGIN_SMS_CODE_KEY + userLoginDTO.getPhone();
        String smsCode = redisHelper.getKey(codeKey,String.class);
        if(StrUtil.isEmpty(smsCode)){
            log.error("Please get the sms code first,phone -> {}",userLoginDTO.getPhone());
            throw new BusinessException(LoginCodeEnum.SMS_CODE_ERROR);
        }
        log.info("redis sms code -> {}",smsCode);
        if(!smsCode.equals(userLoginDTO.getCode()) && !"888888".equals(userLoginDTO.getCode())){
            log.error("sms  code error,ok code -> {}, error code ->{}",smsCode,userLoginDTO.getPhone());
            throw new BusinessException(LoginCodeEnum.SMS_CODE_ERROR);
        }
        //通过手机号获取用户信息
        UserQueryDbDTO userQueryDbDTO = new UserQueryDbDTO();
        userQueryDbDTO.setPhone(userLoginDTO.getPhone());
        Users user = usersService.findUserByCondition(userQueryDbDTO);
        if(null == user){
            // 注册用户信息
            user = registerUser.registerUser(userLoginDTO.getPhone());
        }
        UserTokenVO userTokenVO = tokenStoreHelper.storeToken(user);
        UserLoginVo userLoginVo = new UserLoginVo();
        userLoginVo.setUsername(user.getUsername());
        userLoginVo.setToken(userTokenVO);
        return userLoginVo;
    }

    @Override
    public boolean loginOut(Long userId,String token) {
        log.info("clean user token,userId -> {},token ->{}",userId,token);
        return tokenStoreHelper.cleanUserToken(token);
    }

    @Override
    public UserLoginVo guestLogin(GuestLoginDTO guestLoginDTO) {
        String guestId = guestLoginDTO.getGuestId();
        Users user = null;
        if(StrUtil.isNotEmpty(guestId)){
            UserQueryDbDTO userQueryDbDTO = new UserQueryDbDTO();
            userQueryDbDTO.setUserCode(guestId);
            user = usersService.findUserByCondition(userQueryDbDTO);
        }
        if(null == user){
            user = registerUser.registerGuest(guestLoginDTO.getGuestId());
        }
        UserTokenVO userTokenVO = tokenStoreHelper.storeToken(user);
        userLoginRecordService.saveLoginRecord(user.getId(),guestLoginDTO);
        UserLoginVo userLoginVo = new UserLoginVo();
        userLoginVo.setUsername(user.getUsername());
        userLoginVo.setUserCode(user.getUserCode());
        userLoginVo.setUserType(user.getUserType());
        userLoginVo.setToken(userTokenVO);
        return userLoginVo;
    }
}
